package com.itheima.homework.homeworktest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * 标准版:
 * 新建一个类Student -> String name,Integer chineseScore,
 * Integer mathScore,Integer englishScore.
 * [GET/SET/有参/无参/toString/equals]
 * 使用键盘录入三个学生的数据 -> 12次,通过反射封装三个学生对象.
 * 存储到ArrayList中,将ArrayList中的数据按照主要条件:
 * 总分 次要条件:语文 次要:数学进行排序(升序/降序无所谓) 默认所有学生均不重复.
 * 将ArrayList中的数据按照 姓名:xxx 总分:xxx 语文:xxx 数学:xxx 英语:xxx
 * 一个学生一行写入到当前模块下的student.txt中.
 */
public class HomeWorkClient {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
        Class clazz = Class.forName("com.itheima.homework.homeworktest.Student");
        ArrayList<Student> as = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入第" + (i + 1) + "个学生的姓名");
            String name = sc.nextLine();
            System.out.println("请输入该生的语文成绩:");
            Integer chineseScore = sc.nextInt();
            System.out.println("请输入该生的数学成绩:");
            Integer mathScore = sc.nextInt();
            System.out.println("请输入该生的英语成绩:");
            Integer englishScore = sc.nextInt();
            Constructor constructor = clazz.getDeclaredConstructor(String.class,
                    Integer.class, Integer.class, Integer.class);
            Student student = (Student) constructor.newInstance(name, chineseScore, mathScore, englishScore);
            as.add(student);
        }
        Collections.sort(as, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = (o1.getChineseScore() + o1.getEnglishScore() + o1.getMathScore()) -
                        (o2.getChineseScore() + o2.getEnglishScore() + o2.getMathScore());
                if (result == 0) {
                    result = o1.getChineseScore() - o2.getChineseScore();
                }
                if (result == 0) {
                    result = o1.getMathScore() - o2.getMathScore();
                }
                return result;
            }
        });
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Day16\\student.txt")));
        int count = 0;
        while (count < as.size()) {
            bw.write("姓名:" + as.get(count).getName() + "总分:" + as.get(count).getTotalScore() +
                    "语文:" + as.get(count).getChineseScore() + "数学:" + as.get(count).getMathScore()
                    + "英语:" + as.get(count).getEnglishScore());
            bw.newLine();
            count++;
            bw.flush();
        }
        bw.close();
        Socket socket = new Socket();
        socket.
    }
}
